package rensoft.android.lang;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.UnsupportedEncodingException;

import static rensoft.java.lang.Lang.error;

import android.graphics.drawable.Drawable;
import rensoft.android.HasActivity;
import rensoft.java.event.Event;
import rensoft.java.event.EventImpl;
import rensoft.java.lang.Lang;

public class AndroidLang {
  private static IAplication aplication;

  public static String asString(InputStream input) {
    final char[] buffer = new char[0x10000];
    StringBuilder out = new StringBuilder();
    Reader in;
    try {
      in = new InputStreamReader(input, "UTF-8");
      int read;
      do {
        read = in.read(buffer, 0, buffer.length);
        if (read > 0) out.append(buffer, 0, read);
      } while (read >= 0);
    } catch (UnsupportedEncodingException e) {
      error(e);
    } catch (IOException e) {
      error(e);
    }
    return out.toString();
  }

  public static <T> Event<T> event() {
    return new EventImpl<T>();
  }

  public static IAplication getAplication() {
    return aplication;
  }

  public static Drawable getDrawable(HasActivity hasactivity, int drawable) {
    return hasactivity.activity().getResources().getDrawable(drawable);
  }

  public static void initialize(IAplication aplication) {
    if (AndroidLang.aplication != null) return;
    AndroidLang.aplication = aplication;
    Lang.initialize(new AndroidLangCore());
  }

  public static void sleep(int delay) {
    try {
      Thread.sleep(delay);
    } catch (InterruptedException e) {
      error(e);
    }
  }
}
